#include <QtGui>

#include "cylinderDialog.h"
#include "src/vector3d.h"

CylinderDialog::CylinderDialog(int _id, QWidget *_parent)
    : QDialog(_parent), id(_id){

    setWindowTitle(QString::fromUtf8("Novo cilindro"));

    cylinder = NULL;
    ambientR = ambientG = ambientB = diffuseR = diffuseG = diffuseB = specularR = specularG = specularB = 200;
    centerX = centerY = centerZ = 0.0;
    radius = 1.0;
    height = 1.0;

    createLayout();
}

CylinderDialog::CylinderDialog(CylinderPrimitive *_cylinder, ObjectTreeItem *_item, QWidget *_parent)
    : QDialog(_parent), cylinder(_cylinder), item(_item){

    setWindowTitle(QString::fromUtf8("Modificar objeto ").append(QString::number(_cylinder->getId())));

    const Material &material = cylinder->getMaterial();
    const double *color = material.getAmbient();
    ambientR = color[0] * 255;
    ambientG = color[1] * 255;
    ambientB = color[2] * 255;

    color = material.getDiffuse();
    diffuseR = color[0] * 255;
    diffuseG = color[1] * 255;
    diffuseB = color[2] * 255;

    color = material.getSpecular();
    specularR = color[0] * 255;
    specularG = color[1] * 255;
    specularB = color[2] * 255;

    Vector3D center = cylinder->getCenter();
    centerX = center.x;
    centerY = center.y;
    centerZ = center.z;

    radius = cylinder->getRadius();
    height = cylinder->getHeight();

    createLayout();
}

CylinderDialog::~CylinderDialog(){
    delete ambientPushButton;
    delete diffusePushButton;
    delete specularPushButton;
}

void CylinderDialog::createLayout(){
//    QLabel *centerXLabel = new QLabel(tr("X:"));
//
//    QDoubleSpinBox *centerXDoubleSpinBox = new QDoubleSpinBox();
//    centerXDoubleSpinBox->setRange(-50.0, 50.0);
//    centerXDoubleSpinBox->setSingleStep(1.0);
//    centerXDoubleSpinBox->setValue(centerX);
//    connect(centerXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeCenterX(double)));
//
//
//    QLabel *centerYLabel = new QLabel(tr("Y:"));
//
//    QDoubleSpinBox *centerYDoubleSpinBox = new QDoubleSpinBox();
//    centerYDoubleSpinBox->setRange(-50.0, 50.0);
//    centerYDoubleSpinBox->setSingleStep(1.0);
//    centerYDoubleSpinBox->setValue(centerY);
//    connect(centerYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeCenterY(double)));
//
//
//    QLabel *centerZLabel = new QLabel(tr("Z:"));
//
//    QDoubleSpinBox *centerZDoubleSpinBox = new QDoubleSpinBox();
//    centerZDoubleSpinBox->setRange(-50.0, 50.0);
//    centerZDoubleSpinBox->setSingleStep(1.0);
//    centerZDoubleSpinBox->setValue(centerZ);
//    connect(centerZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeCenterZ(double)));
//
//
//    QHBoxLayout *centerLayout = new QHBoxLayout;
//    centerLayout->addWidget(centerXLabel);
//    centerLayout->addWidget(centerXDoubleSpinBox);
//    centerLayout->addStretch(1);
//    centerLayout->addWidget(centerYLabel);
//    centerLayout->addWidget(centerYDoubleSpinBox);
//    centerLayout->addStretch(1);
//    centerLayout->addWidget(centerZLabel);
//    centerLayout->addWidget(centerZDoubleSpinBox);
//    centerLayout->addStretch(1);
//
//    QGroupBox *centerGroupBox = new QGroupBox(QString::fromUtf8("Centro"));
//    centerGroupBox->setLayout(centerLayout);
//
//
////    QLabel *radiusLabel = new QLabel(tr("Raio:"));
//
//    QDoubleSpinBox *radiusDoubleSpinBox = new QDoubleSpinBox();
//    radiusDoubleSpinBox->setRange(-50.0, 50.0);
//    radiusDoubleSpinBox->setSingleStep(1.0);
//    radiusDoubleSpinBox->setValue(radius);
//    connect(radiusDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRadius(double)));
//
//    QHBoxLayout *radiusLayout = new QHBoxLayout;
////    radiusLayout->addWidget(radiusLabel);
//    radiusLayout->addWidget(radiusDoubleSpinBox);
//    radiusLayout->addStretch(1);
//
//    QGroupBox *radiusGroupBox = new QGroupBox(QString::fromUtf8("Raio"));
//    radiusGroupBox->setLayout(radiusLayout);
//
//
//    QDoubleSpinBox *heightDoubleSpinBox = new QDoubleSpinBox();
//    heightDoubleSpinBox->setRange(-50.0, 50.0);
//    heightDoubleSpinBox->setSingleStep(1.0);
//    heightDoubleSpinBox->setValue(height);
//    connect(heightDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeHeight(double)));
//
//    QHBoxLayout *heightLayout = new QHBoxLayout;
////    heightLayout->addWidget(heightLabel);
//    heightLayout->addWidget(heightDoubleSpinBox);
//    heightLayout->addStretch(1);
//
//    QGroupBox *heightGroupBox = new QGroupBox(QString::fromUtf8("Altura"));
//    heightGroupBox->setLayout(heightLayout);
//
//    QHBoxLayout *radiusHeightLayout = new QHBoxLayout;
//    radiusHeightLayout->addWidget(radiusGroupBox);
//    radiusHeightLayout->addWidget(heightGroupBox);
//


    ambientPushButton = new QPushButton(tr("Cor ambiente"));
    connect(ambientPushButton, SIGNAL(clicked()), this, SLOT(changeAmbient()));

    diffusePushButton = new QPushButton(tr("Cor difusa"));
    connect(diffusePushButton, SIGNAL(clicked()), this, SLOT(changeDiffuse()));

    specularPushButton = new QPushButton(tr("Cor especular"));
    connect(specularPushButton, SIGNAL(clicked()), this, SLOT(changeSpecular()));
    setButtonColor();


    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndCreate()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QVBoxLayout *mainLayout = new QVBoxLayout;
//    mainLayout->addWidget(centerGroupBox);
//    mainLayout->addLayout(radiusHeightLayout);
    mainLayout->addWidget(ambientPushButton);
    mainLayout->addWidget(diffusePushButton);
    mainLayout->addWidget(specularPushButton);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void CylinderDialog::setButtonColor(){
    QString ambientStr;
    if(ambientR + ambientB + ambientG < 400){
        ambientStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        ambientStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    ambientStr.append(QString::fromUtf8("background-color: rgb("));
    ambientStr.append(QString::number(ambientR));
    ambientStr.append(QString::fromUtf8(", "));
    ambientStr.append(QString::number(ambientG));
    ambientStr.append(QString::fromUtf8(", "));
    ambientStr.append(QString::number(ambientB));
    ambientStr.append(QString::fromUtf8(");"));

    ambientPushButton->setStyleSheet(ambientStr);



    QString diffuseStr;
    if(diffuseR + diffuseB + diffuseG < 400){
        diffuseStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        diffuseStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    diffuseStr.append(QString::fromUtf8("background-color: rgb("));
    diffuseStr.append(QString::number(diffuseR));
    diffuseStr.append(QString::fromUtf8(", "));
    diffuseStr.append(QString::number(diffuseG));
    diffuseStr.append(QString::fromUtf8(", "));
    diffuseStr.append(QString::number(diffuseB));
    diffuseStr.append(QString::fromUtf8(");"));

    diffusePushButton->setStyleSheet(diffuseStr);



    QString specularStr;
    if(specularR + specularB + specularG < 400){
        specularStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        specularStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    specularStr.append(QString::fromUtf8("background-color: rgb("));
    specularStr.append(QString::number(specularR));
    specularStr.append(QString::fromUtf8(", "));
    specularStr.append(QString::number(specularG));
    specularStr.append(QString::fromUtf8(", "));
    specularStr.append(QString::number(specularB));
    specularStr.append(QString::fromUtf8(");"));

    specularPushButton->setStyleSheet(specularStr);
}

void CylinderDialog::acceptAndCreate(){
    if(!cylinder){
        cylinder = new CylinderPrimitive(Vector3D(centerX, centerY, centerZ), radius, height, Material(ambientR/255.0, ambientG/255.0, ambientB/255.0, diffuseR/255.0, diffuseG/255.0, diffuseB/255.0, specularR/255.0, specularG/255.0, specularB/255.0), id);

        done(cylinder);
    }else{
        cylinder->setMaterial(Material(ambientR/255.0, ambientG/255.0, ambientB/255.0, diffuseR/255.0, diffuseG/255.0, diffuseB/255.0, specularR/255.0, specularG/255.0, specularB/255.0));
        cylinder->setCenter(Vector3D(centerX, centerY, centerZ));
        cylinder->setRadius(radius);
        cylinder->setHeight(height);
    }

    accept();
    done();
}

void CylinderDialog::changeAmbient(){
    QColor color = QColorDialog::getColor(QColor(ambientR, ambientG, ambientB), this, "Escolha a cor ambiente");
    if (color.isValid()){
        ambientR = color.red();
        ambientG = color.green();
        ambientB = color.blue();
    }
    setButtonColor();
}

void CylinderDialog::changeDiffuse(){
    QColor color = QColorDialog::getColor(QColor(diffuseR, diffuseG, diffuseB), this, "Escolha a cor difusa");
    if (color.isValid()){
        diffuseR = color.red();
        diffuseG = color.green();
        diffuseB = color.blue();
    }
    setButtonColor();
}

void CylinderDialog::changeSpecular(){
    QColor color = QColorDialog::getColor(QColor(specularR, specularG, specularB), this, "Escolha a cor especular");
    if (color.isValid()){
        specularR = color.red();
        specularG = color.green();
        specularB = color.blue();
    }
    setButtonColor();
}

void CylinderDialog::changeCenterX(double _x){
    centerX = _x;
}

void CylinderDialog::changeCenterY(double _y){
    centerY = _y;
}

void CylinderDialog::changeCenterZ(double _z){
    centerZ = _z;
}

void CylinderDialog::changeRadius(double _r){
    radius = _r;
}

void CylinderDialog::changeHeight(double _h){
    height = _h;
}
